package com.android.ouderenvervoer;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

public class LoginActivity extends Activity{
    EditText textFieldUsername, textFieldPassword;
	Button buttonLogin, buttonWatisDit, buttonRegister;
	CheckBox checkOnthouden;
	DatabaseHelper db;
	ArrayList<Object> row;
	
	 public void onCreate(Bundle savedInstanceState)
	    {
		 	super.onCreate(savedInstanceState);
		 	setContentView(R.layout.loginview);
		 	
		 	db = new DatabaseHelper(this);
		 	
	 		setupViews();
	 		addButtonListeners();
	/*	 	row = db.getRowAsArrayMijnGegevens(1);
		 	//maak user aan voor debug
		 	if(row.isEmpty())
		 	{
		 		db.addRowMijnGegevens("Sjoerd", "", "Moret", "0642320441", "0642320441", "sjoerd@mail.com", "19-04-1990", 1, "sjoerd", "moret");
		 	}
		 	
		 	SharedPreferences settings = getSharedPreferences("login1 ", 0);
		 	String username = settings.getString("username", "");
		 	String password = settings.getString("password", "");

		 	
		 	if( row.isEmpty() )//geen gegevens bekend, ga naar registreren
		 	{
		 		Intent registerActivity = new Intent(getBaseContext(), RegistrerenActivity.class);
    			startActivityForResult(registerActivity,0);
		 	}
		 	else if(!row.isEmpty() && username.equals("") && password.equals("") )// gegevens bekend maar nog niet ingelogd, inlogscherm
		 	{
		 		setContentView(R.layout.loginview);
		 		setupViews();
		 		addButtonListeners();
		 	}
		 	else if(!row.isEmpty() && row.get(9).toString().equals(username) && row.get(10).toString().equals(password) ){  //gegevens bekend en al eens ingelogd, naar menu
		 		Intent mainActivity = new Intent(getBaseContext(), MenuActivity.class);
    			startActivityForResult(mainActivity,0);
		 	}
	       
	       */ 
	    }
	 	/**
	     * creates references and listeners for the GUI interface
	     */
	    private void setupViews()
	    {
	        // THE DATA FORM FIELDS
			textFieldUsername 	= (EditText)findViewById(R.id.editTextGebruikersnaamLogin);
			textFieldPassword 	= (EditText)findViewById(R.id.editTextWachtwoordLogin);
	        // THE BUTTONS
	        buttonLogin 		= (Button)findViewById(R.id.buttonInloggen);
	        buttonWatisDit 		= (Button)findViewById(R.id.buttonWatisditLogin);
	        buttonRegister		= (Button)findViewById(R.id.buttonRegisterPage);
	        // CHECKBOX
	        checkOnthouden		= (CheckBox)findViewById(R.id.checkBoxMijOnthouden);
	    }

	    /**
	     * adds listeners to each of the buttons and sets them to call relevant methods
	     */
	    private void addButtonListeners()
	    {
	        buttonLogin.setOnClickListener
	    	(
	    		new View.OnClickListener()
		    	{
					@Override public void onClick(View v) {login();}
				}
	    	);
	 
	        buttonWatisDit.setOnClickListener
	        (
	        	new View.OnClickListener()
		        {
					@Override public void onClick(View v) {popupWatisdit();}
				}
	        );
	        
	        buttonRegister.setOnClickListener
	        (
	        	new View.OnClickListener() {
					@Override public void onClick(View v) { registreren();}
				}
	        );		
	    }
	    
	    private void login(){
	    	String u = textFieldUsername.getText().toString();//    "sjoerd";
	    	String p = textFieldPassword.getText().toString();//	"moret";
	    	
	    	if( row.get(9).toString().equals(u) && row.get(10).toString().equals(p) )// check of gegevens kloppen en log in
	    	{
	    		if( checkOnthouden.isChecked())//als onthouden gechecked is sla gegevens op!
	    		{
			    	SharedPreferences settings = getSharedPreferences("login1", 0);
			    	Editor editor = settings.edit();
			    	editor.putString("username", u);
			    	editor.putString("password", p);
			    	editor.commit();
	    		}
		    	Intent mainActivity = new Intent(getBaseContext(), MenuActivity.class);
				startActivityForResult(mainActivity,0);
	    	}
	    	else //verkeerde log in gegevens geef pop up ofzo
	    	{
	    		textFieldUsername.setError("usernaam fout");
	    		textFieldPassword.setError("password fout");
	    		
	    		textFieldUsername.setText("");
	    		textFieldPassword.setText("");
	    	}
	    }
	    private void popupWatisdit(){
	    	
	    }
	    private void registreren(){
	    	Intent registerActivity = new Intent(getBaseContext(), RegistrerenActivity.class);
			startActivityForResult(registerActivity,0);
	    }
}
